from flask import Flask, jsonify, request
from dpbt import compute
from flask_cors import CORS

app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
# app监听4001端口


@app.route('/diff', methods=['POST'])
def diff():
    arg_a = request.json.get('a')
    arg_b = request.json.get('b')
    if arg_a is None or arg_b is None:
        return jsonify({'error': 'a and b are required'}, 400)
    # 需要加入 [] 的情况防止 '' 在编辑脚本中被作为一行
    diff = compute([] if len(arg_a) == 0 else arg_a.split('\n'),
                   [] if len(arg_b) == 0 else arg_b.split('\n'))
    # 返回diff
    return jsonify({'diffs': [d.__json__() for d in diff]})


app.run(host='0.0.0.0', port=4001, debug=True)
